import { baseUrl } from '../../utils/request'
Page({
  data: {
    name: '',
    nameError: '',
    phone: '',
    phoneError: '',
    email: '',
    emailError: '',
    genderValue: '',
    genderError: '',
    showGenderPopup: false,
    genderColumns: [
      { value: '1', text: '男' },
      { value: '2', text: '女' },
      { value: '0', text: '未知' }
    ],
    genderIndex: 1,
  },
  goBack() {
    wx.navigateBack({
      delta: 1  // delta 表示返回的页面层数，1 即返回上一页
    });
  },
  onNameInput(e) {
    this.setData({
      name: e.detail,
      nameError: ''
    });
  },
  onPhoneInput(e) {
    this.setData({
      phone: e.detail,
      phoneError: ''
    });
  },
  onEmailInput(e) {
    this.setData({
      email: e.detail,
      emailError: ''
    });
  },
  openGenderPopup() {
    this.setData({
      showGenderPopup: true
    });
  },
  closeGenderPopup() {
    this.setData({
      showGenderPopup: false
    });
  },
  onGenderConfirm(e) {
    console.log(e, 'e')
    const { value: {text, index} } = e.detail;
    this.setData({
      genderValue: text,
      genderIndex: index, // 1男，2女，0未知
      showGenderPopup: false,
      genderError: ''
    });
  },
  onSubmit() {
    // 表单验证逻辑
    let hasError = false;
    if (!this.data.name) {
      this.setData({ nameError: '请输入姓名' });
      hasError = true;
    }
    if (!this.data.phone) {
      this.setData({ phoneError: '请输入手机号' });
      hasError = true;
    } else if (!/^1[3-9]\d{9}$/.test(this.data.phone)) {
      this.setData({ phoneError: '手机号格式错误' });
      hasError = true;
    }
    console.log(this.data.email)
    if (!this.data.email) {
      this.setData({ emailError: '请输入邮箱' });
      hasError = true;
    } else if (this.data.email && !/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(this.data.email)) {
      this.setData({ emailError: '邮箱格式错误' });
      hasError = true;
    }
    if (!this.data.genderValue) {
      this.setData({ genderError: '请选择性别' });
      hasError = true;
    }

    if (hasError) return;

    // 验证通过，提交数据
    const formData = {
      actualName: this.data.name,
      phone: this.data.phone,
      loginName: this.data.phone,
      email: this.data.email,
      gender: this.data.genderIndex,
      departmentId: 1,
      disabledFlag: 0,
      leaveFlag: 0
    };

    wx.request({
      url: baseUrl+ '/login/employee/add',
      method: 'POST',
      data: formData,
      success(res) {
        console.log(res, 'res')
        wx.showToast({
          title: '注册成功',
          icon: 'success',
          success() {
            setTimeout(() => {
              wx.navigateBack({
                delta: 1 // 返回上一页
              });
            }, 500)
          }
        });
      },
      fail(err) {
        wx.showToast({
          title: '网络错误，请稍后再试',
          icon: 'none'
        })
      }
    })
  },
  onReset() {
    this.setData({
      name: '',
      nameError: '',
      phone: '',
      phoneError: '',
      email: '',
      emailError: '',
      genderValue: '',
      genderError: '',
    });
  }
});